/**
 * Created by admin on 2017/4/5.
 */
var addMovieApp = new Vue({
    el: '#addMovie',
    data: {
        movie: {},
        file: null,
        post: {},
        message: "",
        alterError: false,
        alterSuccess: false
    },
    computed: {
        classObject: function () {
            return {
                "alter-error": this.alterError,
                "alter-success": this.alterSuccess
            }
        }
    },
    methods: {
        addMovie: function () {
            var formData = new FormData();
            for (key in this.movie) {
                formData.append(key, this.movie[key]);
            }
            formData.append("post", this.post);
            this.$http.post('/rest/movie/addMovie', formData, {
                params: {
                    headers: {
                        'Content-Type': undefined
                    }
                }
            }).then(function (response) {
                if (response && response.data) {
                    var data = response.data;
                    this.message = data.header.resultMessage;
                    if (data.header && data.header.resultCode === "0") {
                        this.alterSuccess = true;
                    }
                }
            });
        },
        addFile: function (event) {
            event.preventDefault();
            $('#post').trigger('click');
            return false;
        },
        addPost: function (event) {
            debugger;
            var files = event.target.files || event.dataTransfer.files;
            if (!files.length) {
                return;
            }

            if (typeof FileReader === 'undefined') {
                alert('您的浏览器不支持图片上传，请升级您的浏览器');
                return false;
            }
            var image = new Image();
            var reader = new FileReader();
            reader.readAsDataURL(files[0]);
            this.post = files[0];
            reader.onload = function (e) {
                addMovieApp.file = e.target.result;
            };
        }
    }
});

var init = function () {
    addMovieApp.movie = {};
    var sessionStorage = window.sessionStorage;
    if (sessionStorage.getItem("movie")) {
        var movieJson = sessionStorage.getItem("movie")
        addMovieApp.movie = JSON.parse(movieJson);
        sessionStorage.removeItem("movie");
    }
}

init();
//Vue.http.options.emulateJSON = true;